package com.common.core.okHttpRequest;

import com.common.core.CoreConfiguration;
import com.common.core.utils.AppUtils;
import com.common.http.basecore.bean.HttpParamsBean;
import com.common.http.basecore.builder.PostFormBuilder;
import com.common.http.basecore.callback.Callback;
import com.common.http.basecore.callback.FileCallBack;
import com.common.http.basecore.manager.OkHttpClientManager;
import com.common.http.basecore.request.RequestCall;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class OkHttpRequest {
    private static Map<String, String> headerMap = null;
    public static void resetHeaderMap(){
        if(headerMap != null){
            headerMap.clear();
        }
        headerMap = null;
    }
    public static Map<String, String> createHeadParams () {
        //添加公共参数到头信息
        if(headerMap == null || headerMap.size() <=0) {
            headerMap = new HashMap<String, String>();
            headerMap.put(AppUtils.APPVERSION, CoreConfiguration.getHeadParamsInfo().getVersion());
            headerMap.put(AppUtils.APPCODE, CoreConfiguration.getHeadParamsInfo().getPcode());
            headerMap.put(AppUtils.DEVICEID, CoreConfiguration.getHeadParamsInfo().getDevid());
        }
        return headerMap;
    }

    private static Map<String, String> getDynamicHeadParams( ArrayList<HttpParamsBean> httpParamsBeans) {
        //添加公共参数到头信息
        createHeadParams();
        if(httpParamsBeans == null || httpParamsBeans.size()<=0){
            return headerMap;
        }
        Map<String, String> headerMapTmp = new HashMap<String, String>();
        headerMapTmp.putAll(headerMap);
        for (HttpParamsBean mHttpParamsBean:httpParamsBeans) {
            headerMapTmp.put(mHttpParamsBean.getKey(), mHttpParamsBean.getValue());
        }
        return headerMapTmp;
    }
    /**
     * post请求
     *
     * @param url
     * @param params
     * @param callback
     * @return
     */
    public static RequestCall postRequest (String url, Map<String, String> params, Callback callback) {
        RequestCall mRequestCall = OkHttpClientManager
                .post()
                .url(url)
                .params(params)
                .headers(createHeadParams())
                .build();
        mRequestCall.execute(callback);
        return mRequestCall;
    }
    /**
     * 带有一个头参数的post请求
     * @param url
     * @param params
     * @param headParams
     * @param callback
     * @return
     */
    public static RequestCall postRequest (String url, Map<String, String> params, HttpParamsBean headParams, Callback callback) {
        RequestCall mRequestCall = OkHttpClientManager
                .post()
                .url(url)
                .params(params)
                .headers(createHeadParams())
                .addHeader(headParams.getKey(), headParams.getValue())
                .build();
        mRequestCall.execute(callback);
        return mRequestCall;
    }
    /**
     * 带有多个头参数的post请求
     * @param url
     * @param params
     * @param headParams
     * @param callback
     * @return
     */
    public static RequestCall postRequest (String url, Map<String, String> params, ArrayList<HttpParamsBean> headParams, Callback callback) {
        RequestCall mRequestCall = OkHttpClientManager
                .post()
                .url(url)
                .params(params)
                .headers(getDynamicHeadParams(headParams))
                .build();
        mRequestCall.execute(callback);
        return mRequestCall;
    }
    /**
     * get请求
     * @param url
     * @param params
     * @param callback
     * @return
     */
    public static RequestCall getRequest (String url, Map<String, String> params, Callback callback) {
        RequestCall mRequestCall = OkHttpClientManager
                .get()
                .url(url)
                .params(params)
                .headers(createHeadParams())
                .build();
        mRequestCall.execute(callback);
        return mRequestCall;
    }
    /**
     * 带有一个头参数的get请求
     * @param url
     * @param params
     * @param headParams
     * @param callback
     * @return
     */
    public static RequestCall getRequest (String url, Map<String, String> params, HttpParamsBean headParams, Callback callback) {
        RequestCall mRequestCall = OkHttpClientManager
                .get()
                .url(url)
                .params(params)
                .headers(createHeadParams())
                .addHeader(headParams.getKey(), headParams.getValue())
                .build();
        mRequestCall.execute(callback);
        return mRequestCall;
    }
    /**
     * 带有多个头参数的get请求
     * @param url
     * @param params
     * @param headParams
     * @param callback
     * @return
     */
    public static RequestCall getRequest (String url, Map<String, String> params, ArrayList<HttpParamsBean> headParams, Callback callback) {
        RequestCall mRequestCall = OkHttpClientManager
                .get()
                .url(url)
                .params(params)
                .headers(getDynamicHeadParams(headParams))
                .build();
        mRequestCall.execute(callback);
        return mRequestCall;
    }
    /**
     * 单文件下载
     *
     * @param url
     * @param mFileCallBack
     * @return
     */
    public static RequestCall downloadFile (String url, FileCallBack mFileCallBack) {
        RequestCall mRequestCall = OkHttpClientManager
                .get()
                .url(url)
                .build();
        mRequestCall.execute(mFileCallBack);
        return mRequestCall;
    }
    /**
     * 单文件上传
     *
     * @param file
     * @param url
     * @param params
     * @param callback
     * @return
     */
    public static RequestCall uploadFile (File file , String url, Map<String, String> params, Callback callback) {
        if (!file.exists()) {
            return null;
        }
        PostFormBuilder postFormBuilder = OkHttpClientManager.post();
        postFormBuilder.addFile("picfile", file.getName(), file);
        RequestCall mRequestCall = postFormBuilder.url(url)
                .params(params)
                .headers(createHeadParams())
                .build();
        mRequestCall.execute(callback);
        return mRequestCall;
    }

    /**
     * 多文件上传
     * @param url
     * @param params
     * @param files
     * @param callback
     * @return
     */
    public static RequestCall multiFileUpload (String url, Map<String, String> params, ArrayList<File> files, Callback callback) {
        if (files == null || files.size() <= 0) {
            return null;
        }
        PostFormBuilder postFormBuilder = OkHttpClientManager.post();
        for (int i = 0; i < files.size(); i++) {
            if (files.get(i).exists()) {
                postFormBuilder.addFile("picfile", files.get(i).getName(), files.get(i));
            }
        }
        RequestCall mRequestCall = postFormBuilder.url(url)
                .params(params)
                .headers(createHeadParams())
                .build();
        mRequestCall.execute(callback);
        return mRequestCall;
    }


    //===========================================================================================
//===========================================================================================
//=====================================Just for test======================================================
//    private static class MyStringCallback extends StringCallback {
//        @Override
//        public void onBefore (Request request, int id) {
//            LogInfo.log("MyStringCallback", "onResponse：onBefore");
//        }
//
//        @Override
//        public void onAfter (int id) {
//            LogInfo.log("MyStringCallback", "onResponse：onAfter");
//        }
//        @Override
//        public void onError (Call call, Exception e, int id) {
//            e.printStackTrace();
//        }
//
//        @Override
//        public void onResponse (String response, int id) {
//            LogInfo.log("MyStringCallback", "onResponse：complete");
//        }
//        @Override
//        public void inProgress (float progress, long total, int id) {
//            LogInfo.log("MyStringCallback", "inProgress:" + progress);
//        }
//    }
//
//    protected static void okHttpTest() {
//        String url = "https://api.douban.com/v2/movie/top250?";
//        Map<String, String> params = new HashMap<String, String>();
//        params.put("start", "0");
//        params.put("count", "10");
//        RequestCall mRequestCall = getRequest(url, params, new MyStringCallback());
//    }
}
